package com.gun.management.module.task.dal.mysql.taskRewardLog;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.mybatis.core.mapper.BaseMapperX;
import com.gun.management.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.gun.management.module.task.controller.admin.taskRewardLog.vo.TaskRewardLogPageReqVO;
import com.gun.management.module.task.controller.admin.taskRewardLog.vo.TaskRewardLogRespVO;
import com.gun.management.module.task.controller.admin.taskRewardRecord.vo.TaskRewardRecordRespVO;
import com.gun.management.module.task.dal.dataobject.taskRewardLog.TaskRewardLogDO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 任务奖励日志信息 Mapper
 *
 * @author 测试账号
 */
@Mapper
public interface TaskRewardLogMapper extends BaseMapperX<TaskRewardLogDO> {

    default PageResult<TaskRewardLogDO> selectPage(TaskRewardLogPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<TaskRewardLogDO>()
                .eqIfPresent(TaskRewardLogDO::getTaskId, reqVO.getTaskId())
                .eqIfPresent(TaskRewardLogDO::getTaskType, reqVO.getTaskType())
                .eqIfPresent(TaskRewardLogDO::getUserId, reqVO.getUserId())
                .eqIfPresent(TaskRewardLogDO::getSubUserId, reqVO.getSubUserId())
                .eqIfPresent(TaskRewardLogDO::getRewardScore, reqVO.getRewardScore())
                .betweenIfPresent(TaskRewardLogDO::getRewardTime, reqVO.getRewardTime())
                 .betweenIfPresent(TaskRewardLogDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(TaskRewardLogDO::getId));
    }



    @Insert("INSERT INTO task_reward_log(`task_id`, `task_type`,`user_id`,`sub_user_id`, task_screenshot,`reward_score`, `reward_time`,  `day_reward_times`, `max_reward_times`, `creator`, `create_time`, `updater`, `update_time` ) VALUES " +
            "(#{entity.taskId},#{entity.taskType}, #{entity.userId},#{entity.subUserId},#{entity.taskScreenshot}, #{entity.rewardScore}, #{entity.rewardTime},  #{entity.dayRewardTimes}, #{entity.maxRewardTimes}, #{entity.creator}, #{entity.createTime}, #{entity.updater}, #{entity.updateTime}  );")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public void insertAndGetId(@Param("entity") TaskRewardLogDO entity);

    @Select({"<script> " +
            "select A.id ,B.id task_id,A.task_type,B.name task_name, B.category_id,A.reward_score,A.user_id,A.sub_user_id,C.nickname nick_name,CONCAT(LEFT(C.mobile, 3), '****', RIGHT(C.mobile, 4)) mobile,  " +
            " A.task_screenshot,A.reward_time,A.create_time, A.update_time from " +
            " task_reward_log A INNER   JOIN  task_info B  ON A.task_id = B.id  " +
            "LEFT JOIN member_user C on A.user_id = C.id   " +
            "<where>" +
            "  A.deleted = 0  " +
            "<if test='taskName != null  and taskName != \"\" '>" +
            " and B.name like   concat('%',#{taskName},'%') " +
            "</if>" +
            "<if test='categoryId != null'>" +
            " and B.category_id =  #{categoryId}  " +
            "</if>" +
            "<if test='rewardTime != null '>" +
            " and A.reward_time  between  #{rewardTime[0]} and #{rewardTime[1]}  " +
            "</if>" +

            "<if test='userName != null  and userName != \"\" '>" +
            " and ( C.nickname like   concat('%',#{userName},'%') or A.user_id like   concat('%',#{userName},'%'))" +
            "</if>" +
            "</where>" +
            " order by A.create_time desc, A.id desc  " +
            "</script>"}
    )
    public IPage<TaskRewardLogRespVO> selectTaskRewardRecordPageList(IPage<?> page, @Param("taskName") String  taskName , @Param("categoryId") Integer categoryId,   @Param("rewardTime") LocalDateTime[] rewardTime, @Param("userName") String  userName );

    @Select({"<script> " +
            "select A.id ,B.id task_id,A.task_type,B.name task_name, B.category_id,A.reward_score,A.user_id,A.sub_user_id,C.nickname nick_name,CONCAT(LEFT(C.mobile, 3), '****', RIGHT(C.mobile, 4)) mobile,  " +
            " A.task_screenshot,A.reward_time,A.create_time, A.update_time from " +
            " task_reward_log A INNER   JOIN  task_info B  ON A.task_id = B.id  " +
            "LEFT JOIN member_user C on A.user_id = C.id   " +
            "<where>" +
            "  A.deleted = 0  " +
            "<if test='userId != null '>" +
            " and A.user_id = #{userId}  " +
            "</if>" +
            "<if test='taskId != null '>" +
            " and B.id = #{taskId}  " +
            "</if>" +
            "<if test='taskName != null  and taskName != \"\" '>" +
            " and B.name like   concat('%',#{taskName},'%') " +
            "</if>" +
            "<if test='categoryId != null'>" +
            " and B.category_id =  #{categoryId}  " +
            "</if>" +
            "<if test='rewardTime != null '>" +
            " and A.reward_time  between  #{rewardTime[0]} and #{rewardTime[1]}  " +
            "</if>" +

            "<if test='userName != null  and userName != \"\" '>" +
            " and ( C.nickname like   concat('%',#{userName},'%') or A.user_id like   concat('%',#{userName},'%'))" +
            "</if>" +
            "</where>" +
            " order by A.create_time desc, A.id desc  " +
            "</script>"}
    )
    public List<TaskRewardLogRespVO> getTaskRewardRecordList(@Param("userId") Long userId, @Param("categoryId") Integer categoryId, @Param("taskId") Long taskId, @Param("taskName") String  taskName , @Param("rewardTime") LocalDateTime[] rewardTime, @Param("userName") String  userName );





}